#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
using namespace std;
int m, n, k;
const int N = 1e4 + 10, M = 500010l;
int e[N], ne[N], h[N], w[N];
int idx;
int dis[N];
bool flag[N];
void add(int a, int b, int c)
{
	w[idx] = c;
	e[idx] = b;
	ne[idx] = h[a];
	h[a] = idx++;
}
void Dij()
{
	memset(dis, 0x3f, sizeof(dis));
	dis[k] = 0;
	for (int j = 0; j < n; j++) {
		int pos = -1;
		for (int i = 1; i <= n; i++) {
			if (!flag[i] && (pos == -1 || dis[i] < dis[pos])) {
				pos = i;
			}
		}
		flag[pos] = 1;
		for (int i = h[pos]; i != -1; i = ne[i]) {
			int j = e[i];
			if (!flag[j] && dis[j] > dis[pos] + w[i]) {
				dis[j] = dis[pos] + w[i];
			}
		}
	}
}
int main()
{
	memset(h, -1, sizeof(h));
	cin >> n >> m >> k;
	while (m--) {
		int a, b, c;
		scanf_s("%d%d%d", &a, &b, &c);
		add(a, b, c);
	}
	Dij();
	for (int i = 1; i <= n; i++) {
		printf("%d ", dis[i]);
	}
	return 0;
}